package com.zzptc.fxh.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private final Retrofit retrofit=new Retrofit.Builder()
            .baseUrl("https://liyuankun.cn/api/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RecyclerView rv = findViewById(R.id.rv);

        MyAppService service = retrofit.create(MyAppService.class);
        Call<RecipeModel> call = service.getRecipe(5);
        call.enqueue(new Callback<RecipeModel>() {
            @Override
            public void onResponse(Call<RecipeModel> call, Response<RecipeModel> response) {
                RecipeModel body = response.body();
                List<RecipeResult> results=body.getResults();
                for (RecipeResult result:
                results){
                    result.save();
                }
                LinearLayoutManager manager = new LinearLayoutManager(MainActivity.this);
                MyAdapter myRvAdapter = new MyAdapter(MainActivity.this);
                rv.setLayoutManager(manager);
                rv.setAdapter(myRvAdapter);
            }

            @Override
            public void onFailure(Call<RecipeModel> call, Throwable t) {
                Log.e(TAG, "onFailure: "+t.getMessage());
            }
        });
    }
}
